package com.sdtele.tta.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.Doc;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.TaskResponse;
import com.sdtele.tta.models.User;

public class TaskUtils {

	public static void TaskAssemble(Task task, Map<String, Object> tMap) {
	
		long taskid = task.getId();
		
		List<Doc>taskDocs = Dac.getInstance().getDocsOfTask(taskid);
		List<TaskResponse> taskResList = Dac.getInstance().getResponse(taskid);
		List<User>taskImplement=Dac.getInstance().getUserbyimplementId(taskid);
		List<User>taskReader=Dac.getInstance().getUserbyreadertId(taskid);
		User owner = Dac.getInstance().getUser(task.getOwnerId());
		tMap.put("owner", owner);
		
		if(taskDocs!=null && (taskDocs.size()>0))
		tMap.put("docs", taskDocs);
		
		if(taskResList!=null && (taskResList.size()>0))
		tMap.put("taskResponse", taskResList);
		
		if(taskImplement!=null && (taskImplement.size()>0))
		tMap.put("implement", taskImplement);
		
		if(taskReader!=null && (taskReader.size()>0))
		tMap.put("readers", taskReader);
		
		//子任务
		List<Task>subTask=Dac.getInstance().getSubTask(taskid);
		List <Map<String,Object>>subTaskAssmble = new ArrayList<Map<String,Object>>();
		if(subTask !=null && (subTask.size()>0))
		{
			for(Task t : subTask)
			{
				
				Map<String, Object> taskMap = new HashMap<String, Object>();
				taskMap = TaskToMap(t);
				TaskAssemble(t,taskMap);
				subTaskAssmble.add(taskMap);
			}
			
		
		}
		tMap.put("subTaskList", subTaskAssmble);
		
	}
	
	
	
	public static Map<String, Object> TaskToMap(Task task) {
		Map<String, Object> taskToMap = new HashMap<String, Object>();
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		taskToMap.put("id", task.getId());
		taskToMap.put("title", task.getTitle());
//		taskToMap.put("ownerId", task.getOwnerId());
		taskToMap.put("description", task.getDescription());
		taskToMap.put("createTime", sdf.format(task.getCreateTime()));
		
		taskToMap.put("emergency", task.getEmergency());
		taskToMap.put("fatherTaskId", task.getFatherTaskId());
		taskToMap.put("completion", task.getCompletion());
		taskToMap.put("state", task.getState());
		taskToMap.put("tags", task.getTags());
		taskToMap.put("updateTime", sdf.format(task.getUpdateTime()));
		
		if( task.getDeadLine() !=null)
			taskToMap.put("deadLine", sdf.format(task.getDeadLine()));
		else
			taskToMap.put("deadLine", "");
		return taskToMap;
	}
}
